package cn.xyinping.org.controller;

import cn.xyinping.basic.util.AjaxResult;
import cn.xyinping.basic.util.PageList;
import cn.xyinping.org.domain.SystemDictionaryDetail;
import cn.xyinping.org.domain.SystemDictionaryType;
import cn.xyinping.org.query.SystemDictionaryDetailQuery;
import cn.xyinping.org.service.ISystemDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 27266
 * @desrciption 数据字典明细
 * @date 2021/10/25
 */
@RestController
@RequestMapping("/detail")
public class SystemDictionaryDetailController {
    @Autowired
    private ISystemDictionaryDetailService detailService;

    // 查一条数据
    @GetMapping("/{id}")
    public SystemDictionaryDetail queryById(@PathVariable("id") long id) {
        return detailService.findOne(id);
    }

    // 查询所有数据
    @GetMapping
    public List<SystemDictionaryDetail> queryAll() {
        return detailService.findAll();
    }

    // 分页查询
    @PostMapping
    public PageList<SystemDictionaryDetail> queryPage(@RequestBody SystemDictionaryDetailQuery query) {
        return detailService.queryList(query);
    }

    // 删除
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") long id) {
        try {
            detailService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙。。"); // 失败
        }
    }

    // 添加和修改
    @PutMapping
    public AjaxResult addAndUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail) {
        try {
            if (systemDictionaryDetail.getId() == null) {
                detailService.add(systemDictionaryDetail);
            } else {
                detailService.update(systemDictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙。。。");
        }

    }
}
